package com.example.music_mediaplayer.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.music_mediaplayer.ApiInfo;
import com.example.music_mediaplayer.R;
import com.example.music_mediaplayer.adapter.MyListViewAdapter;
import com.example.music_mediaplayer.globalvariable.GlobalVariable;
import com.example.music_mediaplayer.service.MyService;
import com.example.music_mediaplayer.tool.LogoutT;
import com.example.music_mediaplayer.tool.ToastTools;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;



public class KuWoFragment extends Fragment {
    private View rootView;
    private ListView listView;
    public MyListViewAdapter adapter;
    private Intent intent;
    private boolean flg = true;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.fragment_kuwo,null);
        }
        initView(rootView);
        return rootView;
    }

    public void initView(View rootView){
        listView = (ListView) rootView.findViewById(R.id.song_list);
        adapter = new MyListViewAdapter(getActivity(), GlobalVariable.KuWoSongList,2);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                OkHttpUtils
                        .get()
                        .url(ApiInfo.kuWoSongUrl)
                        .addParams("rid",GlobalVariable.KuWoSongList.get(i).getId())
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }
                            @Override
                            public void onResponse(String response, int id) {
                                LogoutT.logout(response+"   mp3");
                                if (response!=null&&!response.equals("")){
                                    GlobalVariable.KuWoSongList.get(i).setSongPath(response);
                                    intent = new Intent(getActivity(), MyService.class);
                                    intent.putExtra("url",response);
                                    intent.putExtra("songName",GlobalVariable.KuWoSongList.get(i).getSongName());
                                    intent.putExtra("songArtist",GlobalVariable.KuWoSongList.get(i).getSongArtist());
                                    intent.putExtra("songPhoto","url");
                                    intent.putExtra("msg","");
                                    getActivity().startService(intent);
//                                    if (GlobalVariable.LieBiaoSongList.size()==0){
//                                        GlobalVariable.LieBiaoSongList.add(GlobalVariable.KuWoSongList.get(i));
//                                    }else{
//                                        for (int j = 0;j<GlobalVariable.LieBiaoSongList.size();j++){
//                                            if (GlobalVariable.LieBiaoSongList.get(j).getSongPath().equals(GlobalVariable.KuWoSongList.get(i).getSongPath())){
//                                                flg = true;
//                                                return;
//                                            }else{
//                                                flg = false;
//
//                                            }
//                                        }
//                                        if (flg == false){
//                                            GlobalVariable.LieBiaoSongList.add(GlobalVariable.KuWoSongList.get(i));
//                                        }
//                                    }
                                }else{
                                    ToastTools.toastMsg(getActivity(),"该歌曲不能播放");
                                }

                            }
                        });
            }
        });
    }

}
